#include "hal.h"
#include "relay.h"
#include "inversor_frequencia.h"

volatile uint32_t gVelocidadeInversor;
static PWM_TIMERCFG_Type PWMCfgDat;
static PWM_MATCHCFG_Type PWMMatchCfgDat;
static PINSEL_CFG_Type   PinCfg;
/**********************************************************************************
 * Descrição:
 *
 * Paramêtro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/
void inversor_frequencia_init(uint32_t periodo ,uint8_t velocidade){
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_USVAL;
	PWMCfgDat.PrescaleValue = 1000;  //1ms
	PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	//Inicializa opino de PWM1.6
	PinCfg.Funcnum = 2;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 1;
	PinCfg.Pinnum = 26;
	PINSEL_ConfigPin(&PinCfg);

	/* defien o valor do PWMMR0 = 1000 divisoes = periodo*1ms */
	PWM_MatchUpdate(LPC_PWM1, 0, periodo, PWM_MATCH_UPDATE_NOW);
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

	PWM_ChannelConfig(LPC_PWM1, 6, PWM_CHANNEL_SINGLE_EDGE);

	/* Set up match value */
	PWM_MatchUpdate(LPC_PWM1, 6, (periodo*velocidade/100), PWM_MATCH_UPDATE_NOW);

	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 6;
	PWMMatchCfgDat.ResetOnMatch = DISABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);
	PWM_ChannelCmd(LPC_PWM1, 6, ENABLE);

	PWM_ResetCounter(LPC_PWM1);
        inversor_frequencia_off();

}

/**********************************************************************************
 * Descrição:
 *
 * Paramêtro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/

void inversor_frequencia_on(void){
	PWM_CounterCmd(LPC_PWM1, ENABLE);
	PWM_Cmd(LPC_PWM1, ENABLE);
        relay_active(PORT_MOTORES,RELAY_MOTOR_BRACO);
        relay_active(PORT_MOTORES,RELAY_MOTOR_BLOCO);
        relay_active(PORT_RELAYS_INV,RELAY_HABILITA_INV);
}

/**********************************************************************************
 * Descrição:
 *
 * Paramêtro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/

void inversor_frequencia_off(void){
	PWM_CounterCmd(LPC_PWM1, DISABLE);
	PWM_Cmd(LPC_PWM1, DISABLE);
        relay_deactive(PORT_MOTORES,RELAY_MOTOR_BLOCO);
        relay_deactive(PORT_RELAYS_INV,RELAY_HABILITA_INV);
}

/**********************************************************************************
 * Descrição:
 *
 * Paramêtro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/

void inversor_sentido1(void){
	GPIO_SetValue(PORT_RELAYS_INV,RELAY_SENTIDO_INV);
}

/**********************************************************************************
 * Descrição:
 *
 * Paramêtro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/
void inversor_sentido2(void){
	GPIO_ClearValue(PORT_RELAYS_INV,RELAY_SENTIDO_INV);
}
